<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="cn.school.mybatis.utils.MyBatisUtil" %>
<%@ page import="cn.school.mybatis.dao.AdminDao" %>
<%@ page import="cn.school.mybatis.pojo.Admin" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.school.mybatis.service.AdminService" %><%--
  Created by IntelliJ IDEA.
  User: zhaoyang
  Date: 2020/8/5
  Time: 6:31 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录操作</title>
</head>
<body>

<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    // 查看cookie
    Cookie[] cookies = request.getCookies();
    String userName = "";
    boolean flag = true;
    for (int i = 0; cookies != null && i < cookies.length; i++) {
        if (cookies[i].getName().equals("username")) {
            userName = cookies[i].getValue();
            flag = false;
            break;
        }
    }
    if (flag) {
        Cookie c = new Cookie("userName", username);
        c.setMaxAge(60 * 60 * 24 * 30);
        c.setPath("/");
        response.addCookie(c);
    }

    //判断账号密码是否正确
    Admin checkAdmin = AdminService.ADMIN_SERVICE.getUser(username, password);
    if (checkAdmin != null) {
        session.setAttribute("USER_LOGIN", checkAdmin);
        out.println("<script>alert('" + checkAdmin.getUsername() + "欢迎登录！');" +
                "location.replace('" + request.getContextPath() + "/AdminLTEdemo01/s_login/welcome.jsp')</script>");

    } else {


%>
<script>
    location.replace("<%=request.getContextPath()%>/AdminLTEdemo01/s_login/loginFail.jsp");
</script>

<%
    }
%>

</body>
</html>
